#include <stdio.h>
#include <stdlib.h>
#include <term.h>
#include <termio.h>
#include <unistd.h>

namespace Share {
// 按键监听
int ScanKeyboard() {
  int            in;
  struct termios new_settings;
  struct termios stored_settings;
  tcgetattr(0, &stored_settings);
  new_settings = stored_settings;
  new_settings.c_lflag &= (~ICANON);
  new_settings.c_cc[VTIME] = 0;
  tcgetattr(0, &stored_settings);
  new_settings.c_cc[VMIN] = 1;
  tcsetattr(0, TCSANOW, &new_settings);

  in = getchar();

  tcsetattr(0, TCSANOW, &stored_settings);
  return in;
}

// 清屏
void Clear() {
  if (!cur_term) {
    int result;
    setupterm(NULL, STDOUT_FILENO, &result);
    if (result <= 0) return;
  }

  putp(tigetstr("clear"));
  return;
}
} // namespace Share
